package DianShang_2024.ds_02.extract

import org.apache.hudi.DataSourceWriteOptions.{PARTITIONPATH_FIELD, RECORDKEY_FIELD}
import org.apache.hudi.QuickstartUtils.getQuickstartWriteConfigs
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.{col, date_format, greatest, lit, when}
import shapeless.syntax.typeable.typeableOps

import java.util.Properties

object extract01 {
  def main(args: Array[String]): Unit = {
    /*
          1、抽取shtd_store库中user_info的增量数据进入Hudi的ods_ds_hudi库中表user_info。根据ods_ds_hudi.user_info表
          中operate_time或create_time作为增量字段(即MySQL中每条数据取这两个时间中较大的那个时间作为增量字段去和ods里的这两个字段中较
          大的时间进行比较)，只将新增的数据抽入，字段名称、类型不变，同时添加分区，若operate_time为空，则用create_time填充，分区字段
          为etl_date，类型为String，且值为当前比赛日的前一天日期（分区字段格式为yyyyMMdd）。id作为primaryKey，operate_time作
          为preCombineField。使用spark-shell执行show partitions ods_ds_hudi.user_info命令，将结果截图粘贴至客户端
          桌面【Release\任务B提交结果.docx】中对应的任务序号下；
     */

    val spark=SparkSession.builder()
      .master("local[*]")
      .appName("数据抽取第一题")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .enableHiveSupport()
      .getOrCreate()

    val mysql_connect=new Properties()
    mysql_connect.setProperty("user","root")
    mysql_connect.setProperty("password","123456")
    mysql_connect.setProperty("driver","com.mysql.jdbc.Driver")

    spark.sql("use ods_ds_hudi02")


    val max_time=spark.sql(
      """
        |select
        |if(operate_time > create_time,operate_time,create_time) as time
        |from user_info
        |order by time desc
        |""".stripMargin).collect()(0).get(0).toString

    println(max_time)

    println("插入数据前的数量")
    spark.sql("select count(*) from user_info").show


    val user_info_path="hdfs://192.168.40.110:9000/user/hive/warehouse/ods_ds_hudi02.db/user_info"

    //  greatest:求两个字段中较大的值
    //  然后最后要记得将时间类型的数据的数据类型换成字符串类型，否则hudi表不显示
    spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_store?useSSL=false","user_info",mysql_connect)
      .withColumn(
        "operate_time",
        when(col("operate_time").isNull,col("create_time")).otherwise(col("operate_time"))
      )
      .where(
        greatest(col("operate_time"),col("create_time"))  > lit(max_time).cast("timestamp")
      )
      .withColumn("create_time",date_format(col("create_time"),"yyyy-MM-dd HH:mm:ss"))
      .withColumn("operate_time",date_format(col("operate_time"),"yyyy-MM-dd HH:mm:ss"))
      .withColumn("etl_date",lit("20240101"))
      .write.mode("append")
      .format("hudi")
      .options(getQuickstartWriteConfigs)
      .option(RECORDKEY_FIELD.key(),"id")
      .option(PARTITIONPATH_FIELD.key(),"operate_time")
      .option(PARTITIONPATH_FIELD.key(),"etl_date")
      .option("hoodie.table.name","user_info")
      .save(user_info_path)


    println("插入数据后的数量")
    spark.sql("select count(*) from user_info").show


    spark.close()


  }

}
